package com.zhai.test;

import com.alibaba.fastjson.JSON;
import com.zhai.infrastructure.persistent.redis.IRedisService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RMap;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApiTest {

    @Resource
    IRedisService redisService;

    @Test
    public void test() {
        RMap<Object, Object> map = redisService.getMap("strategy_id_100001");
        map.put(1, 101);
        map.put(2, 101);
        map.put(3, 101);
        map.put(4, 102);
        map.put(5, 102);
        map.put(6, 102);
        map.put(7, 103);
        map.put(8, 103);
        map.put(9, 104);
        map.put(10, 105);
        log.info("测试结果：{}", redisService.getFromMap("strategy_id_100001", 10).toString());
    }

    @Test
    public void test2() {
        List<String> list = new ArrayList<>();
        list.add("jlkk");
        list.add("lopi");
        list.add("小傅哥");
        list.add("e4we");
        list.add("alpo");
        list.add("yhjk");
        list.add("plop");

        String[] strs = new String[8];
        for (String s : list) {
            int index =  s.hashCode() & (strs.length - 1);
            System.out.println("index:" + index);
            if (strs[index] == null) {
                strs[index] = s;
                continue;
            }
            strs[index] += strs[index] + "->" + s;
        }
        System.out.println(JSON.toJSONString(strs));
    }

    @Test
    public void test3() {
        List<String> list = new ArrayList<>();
        list.add("jlkk");
        list.add("lopi");
        list.add("小傅哥");
        list.add("e4we");
        list.add("alpo");
        list.add("yhjk");
        list.add("plop");

// 定义要存放的数组
        String[] tab = new String[8];

// 循环存放
        for (String key : list) {
            int idx = (key.hashCode() ^ key.hashCode()>>>16) & (tab.length - 1);  // 计算索引位置
            System.out.println(String.format("key值=%s Idx=%d", key, idx));
            if (null == tab[idx]) {
                tab[idx] = key;
                continue;
            }
            tab[idx] = tab[idx] + "->" + key;
        }
// 输出测试结果
        System.out.println(JSON.toJSONString(tab));
    }

}
